package com.su.test.testdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

public class surface extends SurfaceView implements SurfaceHolder.Callback {

    SurfaceHolder sfholder;

    Camera camera;

    Bitmap bitmap;

    public surface(Context context) {
        super(context);

        sfholder = getHolder();
        sfholder.addCallback(this);
        sfholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public surface(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public surface(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub
        Camera.Parameters parameters = camera.getParameters();
        List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.setPictureSize(720, 1280);
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        camera.setDisplayOrientation(90);
//        camera.autoFocus(new Camera.AutoFocusCallback() {
//            @Override
//            public void onAutoFocus(boolean success, Camera camera) {
//                if (success){
//                    camera.cancelAutoFocus();
//                    Toast.makeText(getContext(),"对焦成功",Toast.LENGTH_SHORT).show();
//                }
//            }
//        });
        camera.setParameters(parameters);
        camera.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub

        try {
            camera = Camera.open(0);
            camera.setPreviewDisplay(sfholder);
        } catch (Exception e) {
            camera.release();
            camera = null;
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera = null;

    }

    //拍照
    public void takePicture() {
        if (camera == null){
            camera = Camera.open(2);
        }
        if (camera != null) {
            camera.takePicture(null, null, jpegCallback);
        }
    }

    //拍照并且保存的回调函数

    private Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub
            try {
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                File file = new File("/sdcard/camera1.jpg");
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
                bos.flush();
                bos.close();
                Canvas canvas = sfholder.lockCanvas();
                canvas.drawBitmap(bitmap, 0, 0, null);
                sfholder.unlockCanvasAndPost(canvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}